const wechat = require('co-wechat');
import { IRouterContext } from "koa-router";
import * as config from "config";
import { api } from "../util/wechat";
const wechatConfig = config.get('wechat');
export const reply = wechat(wechatConfig).middleware(async (message: any, ctx: any) => {
    if (message.MsgType == "text") {
        if (message.Content == "1") {
            // "uqGLmq3yTFfD0Z6kSCEQez4AH9dcJJCmY-VO79ahSaI"
            return [
                {
                    title: '帮助文章',
                    description: '帮助文章',
                    picurl: 'http://mmbiz.qpic.cn/mmbiz_jpg/1THBmibhepcj4ttJrX7xuiajibEGatU8JUibquzFSOiaXvIp76KQOl97gfjbdh3thF6I7dBcAFrLnb39GwZtiaDVq2Yw/0?wx_fmt=jpeg',
                    url: 'https://mp.weixin.qq.com/s?__biz=MzU0ODA2OTg0MQ==&tempkey=OTI1X3BLS2lkWGdRS2hlVUx6ajJ3djBydWlkTXRyU0tFajBlS011YkZxT29LMGRTb0E4QWNoVi1LNVFFZUIyRnNXbnVNcndXNEhLaEVFelEzQlFwbzZIaVA1djNPaDltTnlkZDhlaTNkYXJWcnRNanNnTTdmampKdEZ1RW41ck81UDZwNHdmdm5VdTBETi1rNjRDMXlzTHY0Slo5ZWxMV1RWd29nbjdpMUF%2Bfg%3D%3D&chksm=7b4589b54c3200a3da3a178681a8de92b58cc304e2eeb8ff00e0e0fc033e8e89df55a7c2a571#rd'
                }
            ];
        } else if (message.Content == "0") {
            return {
                type: "image",
                content: {
                    mediaId: "uqGLmq3yTFfD0Z6kSCEQe6lJv8xOJ2v1EAAA28iN7CM"
                }
            };
        }
    } else if (message.MsgType == "event") {
        if (message.Event == "subscribe") {
            //帮助文档ok后，回复1-帮助，0-客服
            return "蓝豚，专注社群\n回复0-客服";
        }
    }
    return "";
})

export const getJsConfig = async (ctx: IRouterContext) => {
    const url = ctx.query.url;
    const jsApiList = ctx.query.jsApiList;
    const debug = ctx.query.debug;
    const jsConfigParam = {
        debug: debug || true,
        jsApiList: jsApiList || [],
        url: url || config.get('fontRootUrl')
    };
    try {
        ctx.body = await api.getJsConfig(jsConfigParam)
    } catch (error) {
        console.log('getJsConfig error:', error);
        throw error;
    }
}